<template>
  <el-row type="flex" :style=" isRoot ? 'height:40px;':'width:100%;' " justify="space-between">
    <!-- 左边 -->
    <el-col>
      <span>{{ treeData.name }}</span>
    </el-col>
    <!-- 右边 -->
    <el-col :span="6">
      <el-row type="flex" justify="space-between">
        <!-- 右1 -->
        <el-col>{{ treeData.manager }}</el-col>
        <!-- 右2 -->
        <el-col>
          <el-dropdown>
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="addHandler">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="editHandler">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delHandler">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>
<script>
import { delDepartmentData } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    // 外部传入的这行的数据
    treeData: {
      type: Object,
      default: () => ({})
    },
    // 是否为顶级
    isRoot: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    // 删除部门
    async delHandler() {
      // 1. 弹出确认框
      await this.$confirm('确定删除吗')
      // 2. 发送删除请求
      await delDepartmentData(this.treeData.id)
      // 3. 提示成功
      this.$message.success('删除成功')
      // 4. 更新列表
      this.$emit('update')
    },
    // 添加子部门
    addHandler() {
      // 通知父组件打开弹框
      this.$emit('addDept', this.treeData)
    },
    // 编辑部门
    editHandler() {
      // console.log(1111);
      this.$emit('editDept', this.treeData)
    }
  }
}
</script>
<style>

</style>
